Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Typing to decorator validators #135

Closed
wants to merge 1 commit into from

Conversation

maxtheman
Copy link

Hi Orsinium (and team)

As I was testing contract, Pylance was giving me type errors due to validator not being typed.

Before typing:
Screenshot 2024-03-24 at 12 35 22 PM

This is not such a nice DX for first-time users using strict typing, so I wanted to fix it.

I think the output of a validator is always supposed to be bool, making this a simple fix.

After typing:
Screenshot 2024-03-24 at 12 35 34 PM

@orsinium
Copy link
Member

Thank you for your contribution! Unfortunately, the contracts are quite dynamic and can't be that easily annotated.

For type checking in mypy, use the mypy plugin that comes with deal. I'd be happy to provide a plugin for pylance and pyright but pyright doesn't support plugins and not going to.

I'm still not sure what to do with the external validators support. I'm considering removing it altogether and just sticking to Callable contracts but external validators might be very handy for advanced payload validation in services. Maybe it doesn't matter much now that we have pydantic widely used, though, especially in FastAPI services.

@maxtheman
Copy link
Author

@orsinium understood, thank you for the continued education here!

I didn't grok the external schema support when I read the docs first — that's actually neat feature for advanced payload validation, like you're saying.

Say I had something like:

`class UnvalidatedFileMetadata(SQLModel):
user_id: UUID = Field(default=None, foreign_key="user.id")
file_name: ValidString = Field(default=None)
file_path: ValidString = Field(default=None)

class FileMetadata(UnvalidatedFileMetadata, table=True):
tablename = "file_metadata" # type: ignore
id: UUID = Field(default_factory=uuid4, primary_key=True)
created_at: datetime = Field(default=datetime.utcnow(), nullable=False)
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)`

Using sqlmodel from the fastapi ecosystem. I could imagine using deal to validate that the input is specifically UnvalidatedFileMetadata at runtime to catch if devs try to pass FileMetadata to a function, thus accidentally skipping validation. May be a bit niche, but that's where my head goes.

Closing this PR, thanks again.

@maxtheman maxtheman closed this Mar 25, 2024
@orsinium
Copy link
Member

Thank you for your work! I'll see if the type annotations still can be improved a bit. I'll ping you for review when and if I have something :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants